package com.kelvin.paopao.widgets.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by mc on 2017/3/8.
 * 支付聊天dialog
 */

public class PayChatDialogFragment extends DialogFragment {
    @BindView(R.id.et_send_money)
    EditText etSendMoney;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    private Activity activity;
    private String price, user_no;

    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);
        this.activity = activity;
    }

    public static PayChatDialogFragment newInstance(String price, String user_no) {
        PayChatDialogFragment f = new PayChatDialogFragment();

        Bundle args = new Bundle();
        args.putString("price", price);
        args.putString("user_no", user_no);
        f.setArguments(args);

        return f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.animate_dialog);

        View view = inflater.inflate(R.layout.dialog_pay_chat, container);
        ButterKnife.bind(this, view);


        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.y = -100;
        getDialog().getWindow().setAttributes(params);

        etSendMoney.requestFocus();
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        etSendMoney.setHint("请购买时间 " + price + "元/小时");
        etSendMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString()) && !s.toString().equals("0")) {
                    BigDecimal bigDecimal = new BigDecimal(price);
                    BigDecimal totalDecimal = bigDecimal.multiply(new BigDecimal(s.toString()));
                    tvTotal.setText("合计：￥" + totalDecimal.doubleValue() + "元");
                }
            }
        });

        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        price = getArguments().getString("price");
        user_no = getArguments().getString("user_no");
    }

    @OnClick(R.id.tv_confirm_pay)
    public void onClick() {
        String hours = etSendMoney.getText().toString();
        if (TextUtils.isEmpty(hours)) {
            return;
        }
        RetrofitService.payTalk(user_no, hours).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    ToastUtil.showToast(getContext(), "支付成功");
                    dismiss();
                    if (listener != null) {
                        listener.onPayComplete();
                    }
                } else {
                    ToastUtil.showToast(getContext(), commonListResponse.getReason());
                }
            }
        });
    }

    private OnPayCompleteListener listener;

    public void setListener(OnPayCompleteListener listener) {
        this.listener = listener;
    }

    public interface OnPayCompleteListener {
        void onPayComplete();
    }
}
